define(['modules','jquery'],function(controllers,$){
    controllers.controller('tabsController',function($scope,$location){
      tabs('tab','content','click',false,true,2000,'tab_class')
      function tabs (tabs,contents,trigger,handler,autoPlay,playtime,currentClass){
      var tabs = document.getElementsByClassName('tab')
      var contents = document.getElementsByClassName('content')
      trigger = trigger || "click";//默认click事件
      playtime = playtime || 2000;//自行切换间隔时间，默认2000毫秒
      var currentIndex = 0;
      // tabs自行切换回调函数
        function playHandler(){
        if($location.$$path==='/tabs'){
           currentIndex++;
        if (currentIndex>=tabs.length) {
          currentIndex = 0;
        }
        for (var j = contents.length-1; j >= 0; j--) {
            contents[j].style.display = 'none';
          }
         contents[currentIndex].style.display = 'block';
         $(tabs).removeClass(currentClass)
         $(tabs[currentIndex]).addClass(currentClass)
        }
        if($location.$$path!=='/tabs'){clearInterval(s)}
        }

        if(autoPlay){
          var s = setInterval(playHandler,playtime)
        }
        // tabs触发事件
        if(trigger){
          for (var i = tabs.length - 1; i >= 0; i--) {
            tabs[i]._index = i;
            // tabs注册事件
           tabs[i].addEventListener(trigger,function(){
          for (var j = contents.length-1;j>=0; j--) {
                contents[j].style.display = 'none';
              }
              contents[this._index].style.display = 'block';
              // 当前content触发的回电函数
              if(handler){
                function a(currentIndex){console.log(currentIndex+10)}
                a(currentIndex)
              }
              // 点击触发class
              if(currentClass){
                $(tabs).removeClass(currentClass)
                $(tabs[this._index]).addClass(currentClass)
              }
              currentIndex = this._index
            })          
          }
        }
    };
    })
})
